﻿using System;
using NUnit.Framework;
using kursovaya.Windows;
using System.Linq;
using System.Threading;
using kursovaya.Data.Context;
using kursovaya.Data.Entity;
using System.Windows;

namespace kursovaya.Tests
{
    [TestFixture]
    public class FirstTest
    {
        private AddTecWindow _addTecWindow;

        [SetUp]
        [Apartment(ApartmentState.STA)] 
        public void SetUp()
        {
            // Загрузка ресурсов Material Design
            var application = new Application();
            application.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml")
            });
            application.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/materialdesign2.defaults.xaml")
            });
            application.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri("pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/materialdesigncolor.bluegrey.xaml")
            });


            // Создание окна AddTecWindow
            _addTecWindow = new AddTecWindow();
        }

        [Test]
        [Apartment(ApartmentState.STA)]
        public void AddTecWindow_AddRecord_SuccessfullyAddsToDatabase()
        {
            var firstName = "Иван";
            var lastName = "Петров";
            var middleName = "Сергеевич";
            var age = "35";
            var role = "Инженер";
            var workExperience = "10 лет";

            _addTecWindow.FirstNameTextBox.Text = firstName;
            _addTecWindow.LastNameTextBox.Text = lastName;
            _addTecWindow.MiddleNameTextBox.Text = middleName;
            _addTecWindow.AgeTextBox.Text = age;
            _addTecWindow.RoleTextBox.Text = role;
            _addTecWindow.WorkExpirienceTextBox.Text = workExperience;
            
            _addTecWindow.AddRecordButton_OnClick(null, null);
            
            using (var db = new DbContext())
            {
                var addedTechnician = db.Technicians.FirstOrDefault(t => t.FirstName == firstName &&
                                                                         t.LastName == lastName &&
                                                                         t.Middlename == middleName &&
                                                                         t.Age == age &&
                                                                         t.Role == role &&
                                                                         t.Workexperience == workExperience);

                Assert.That(addedTechnician, Is.Not.Null, "Данные должны быть успешно добавлены в базу данных");
            }
        }

        [TearDown]
        [Apartment(ApartmentState.STA)]
        public void TearDown()
        {
            _addTecWindow.Close();
        }
    }
}
